package org.bright.controller;

import org.bright.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

//@Controller
@RequestMapping("user")
//@ResponseBody
//Controller + ResponseBody = RestController
@RestController
public class UserController {
    @RequestMapping("getTest")
    public String getTest(){
        return "hi hello  你好，傻鸟小丁丁";
    }




    @GetMapping("getUser")
    public String getTest(@RequestParam("name1") String name,@RequestParam("age1") int age){
        System.out.println(name+age);
        return name+age;
    }

    @GetMapping("postPrams")
    public String postPrams(User user){
        System.out.println(user);
        return String.valueOf(user);
    }

//    接收数组
    @GetMapping("arraysParams")
    public String arraysParams(String[] hobbies){
        System.out.println("接收数组请求参数"+ Arrays.toString(hobbies));
        return "接收数组请求参数";
    }

//    接收集合

    @GetMapping("listParams")
    public String listParams(@RequestParam List<String> hobbies){
        System.out.println("接收数组请求参数"+ hobbies.toString());
        return "接收对象请求参数";
    }

//    接收前端json数据
    @GetMapping("jsonParams")
    public String jsonParams(@RequestBody User user){
        System.out.println("json数据传递"+ user);
        return "接收前端json数据";
    }

//    接收多个json参数
    @PostMapping("postParams")
    public String postParams(@RequestBody List<User> user){
        System.out.println("json数据传递");
        user.forEach(System.out::println);

        return "接收前端json数组";
    }

//    接收日期格式数据 默认格式YYYY/MM/dd
    @GetMapping("dateParams")
    public String dateParams(Date date){
        System.out.println("接收日期格式数据" + date);
        return "接收日期格式数据";
    }

    //接收指定格式日期参数
    @GetMapping("dateParams2")
    public String dateParams2(Date date,@DateTimeFormat(pattern = "yyyy-MM-dd") Date date1){
        System.out.println("接收默认日期格式数据" + date1);
        System.out.println("接收指定日期格式数据1" + date);
        return "接收日期格式数据";
    }
}
